<?php
class Entity extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity_model');
    }

    public function index()
    {
        $tables = $this->db->list_tables();
        foreach ($tables as $table)
        {
           $this->createEntity($table);
        } 
    } 
    
    private function createEntity($tableName)
    {
        $nTable = str_replace('_', ' ', $tableName);
        $nTable = ucwords($nTable);
        $nTable = str_replace(' ','_', $nTable);
        
        //$nTable = ucfirst($tableName);
        $fields = $this->db->field_data($tableName);
        $str = '<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class '.$nTable.'_Entity extends CI_Model
{
    private $tablename = "'.$tableName.'";
    ';
    // properties
        foreach ($fields as $field)
        {
           $str .= 'private $'.$field->name.' = null;
    ';
        }
    // set & get properties
        foreach ($fields as $field)
        {
            $fieldName = ucfirst($field->name);
           $str .= '
    public function set'.$fieldName.'($'.$fieldName.')
    {
        $this->'.$field->name.' = $'.$fieldName.';
    }
';
        }
     // functions 
        $arrFK = $this->entity_model->FKKey($tableName); 
        $arrPK = $this->entity_model->PKKEY($tableName);
        
        $str .= $arrFK[0];
        $str .= $arrPK[0];
        $str .= '
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == "tablename") continue;
            if ($val === null || $val === "") continue;
            $params[$key] = $val;
        }
        ';

        $str .= $arrFK[1];
        
        $str .= '        
        if (!isset($params["id"])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array("id" => $params["id"]));
        }
        
    }
    
    public function del($id)
    {
        '.$arrPK[1].'
        return $this->db->delete($this->tablename, array("id" => $id));    
    }
    
    public function get'.$nTable.'ById($id)
    {
        $query = $this->db->get_where($this->tablename, array("id" => $id));
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        return false;
    }
    
    public function getAllList()
    {
        $query = $this->db->get($this->tablename);
        return $query->result();
    }
    
    public function Total'.$nTable.'()
    {
        $query = $this->db->count_all_results("'.$nTable.'"); 
        return $query->result();
    } 
}       
        ';
        
    $str = $this->opt($str);    
    //echo $str;  
    $dir = ROOT_DIR_PATH.'/'.APPPATH.'models/';
    $dirEntity = $dir.'Entity/'.$nTable.'_Entity.php';
    $mName = strtolower($nTable);
    $dirModel = $dir.$mName.'_model.php';
    $this->load->helper('file');
    write_file($dirEntity, $str);
    echo $dirEntity. ' has created! <br />';
    if (!file_exists($dirModel)) {
        $mStr = '<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
require_once("Entity/'.$nTable.'_Entity.php");        
class '.$nTable.'_Model extends '.$nTable.'_Entity
{  
    public function __construct()
    {
        parent::__construct();
    }
}';
        $mStr = $this->opt($mStr);
        write_file($dirModel, $mStr);
        echo $dirModel. ' has created! <br />';
    }
    }
    
    private function opt($str)
    {
        return str_replace('"', "'", $str);
    }
}